﻿@page "/Pods"
@using BlazorApp.Pages.Common
@using BlazorApp.Pages.Common.Html.HTable
@using BlazorApp.Pages.Common.Metadata
@using BlazorApp.Pages.Container
@using Extension.k8s
@using k8s.Models
@inherits BlazorApp.Pages.Common.TableBase<V1Pod>


@if (ItemList != null)
{
    <HTable Class="wideTable">
        <HTr>
            <HTd >
                <SearchToolBar Title=@($"{L["Pod"]}") Count="@TableData.Total"
                               OnItemDeletedCallback="@(async () => await InvokeAsync(StateHasChanged))"
                               OnSelectedItemCloseClicked="@TableData.RemoveSelection"
                               OnRemoveAllClicked="@TableData.RemoveAllSelection"
                               TableData="@TableData"
                               OnSearch="OnSearchHandler" TItem="V1Pod"
                               OnNsSelected="OnNsSelectedHandler">
                    @if (_metricsServerReady)
                    {
                        <GridRow Class="grid-table">
                            <GridCol Span="2">
                                <Text Strong>@L["Top"]</Text>
                            </GridCol>
                            <GridCol Span="22">
                                <SimpleSelect Style="width:100px;" Placeholder="@L["Sort By"]"
                                              OnSelectedItemChanged="OnSortByChanged">
                                    <SelectOptions>
                                        <SimpleSelectOption Value="cpu" Label="cpu"></SimpleSelectOption>
                                        <SimpleSelectOption Value="memory" Label="memory"></SimpleSelectOption>
                                    </SelectOptions>
                                </SimpleSelect>
                                <Checkbox bind-Value="@_showContainer"
                                          OnChange="@(() => _showContainer = !_showContainer)">
                                    @L["Show Container"]
                                </Checkbox>
                                <Button Type="@ButtonType.Primary" OnClick="@(() => OnTopClick())">@L["Ok"]</Button>
                            </GridCol>
                        </GridRow>
                    }
                </SearchToolBar>
            </HTd>
        </HTr>
        <HTr>
            <HTd >
                <Table TItem="V1Pod"
                       DataSource="@TableData.PagedItems"
                       Size=@TableSize.Small Bordered="true"
                       Total="TableData.Total"
                       @bind-PageIndex="TableData.PageIndex"
                       @bind-PageSize="TableData.PageSize"
                       @bind-SelectedRows="TableData.SelectedRows"
                       OnChange="OnPageChangeHandler">
                    <Selection Key="@(context.Name())" Width="40" Fixed="left"/>
                    <AntDesign.Column Field="@context.Name()" Title=@L["Name"] Width="200px">
                        <a onclick="@(() => OnPodClick(context))">@context.Name()</a>
                    </AntDesign.Column>
                    <AntDesign.Column Field="@context.Namespace()" Title=@L["Namespace"] Width="110px"/>
                    <AntDesign.Column Title=@L["Containers"] TData="string" Width="100px">
                        <ContainerStatus Pod="@context"></ContainerStatus>
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["Restart"] TData="string" Width="80px">
                        @context.RestartCount()
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["ControllerBy"] TData="string">
                        <ControllerBy Owner="@context.OwnerReferences()" Namespace="@context.Namespace()"></ControllerBy>
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["Node"] TData="string" Width="120px">
                        <div onclick="@(() => OnNodeNameClick(context.Spec.NodeName))">@context.Spec.NodeName</div>
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["PodIP"] TData="string" Width="100px">
                        <ColorfulTag> @context.Status.PodIP</ColorfulTag>
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["QoS"] TData="string" Width="100px">
                        @context.Status.QosClass
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["Selector"] TData="string">
                        <SelectorView Selector="@context.Metadata.Labels"></SelectorView>
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["Age"] TData="string" Width="100px">
                        <AgeView Age="@context.CreationTimestamp()"></AgeView>
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["Status"] TData="string" Width="100px">
                        <StatusView Status="@context.Status()"></StatusView>
                    </AntDesign.Column>
                    <ActionColumn Title=@L["Action"] Width="100px">
                        <PodAction PodItem="@context"></PodAction>
                    </ActionColumn>
                </Table>
            </HTd>
        </HTr>
    </HTable>

    <ResourceWatcher OnResourceChanged="OnResourceChanged" T="V1Pod"></ResourceWatcher>
}
